<template>
  <div class="site-wrapper site-page--login">
    <div class="site-content__wrapper">
      <div class="site-content">
        <div class="login-main">
          <div class="leftBg">
            <div class="leftTit">
              欢迎来到
              <img src="@/assets/img/right.png" />
            </div>
            <div class="linse"></div>
            <div class="mainTit">常茂生物登录系统</div>
          </div>
          <div style="flex:1">
            <img class="logoImg" src="@/assets/img/logo.png" alt />
            <el-form
              class="logFrom"
              :model="dataForm"
              :rules="dataRule"
              ref="dataForm"
              @keyup.enter.native="dataFormSubmit()"
              status-icon
            >
              <el-form-item prop="userName">
                <el-input v-model="dataForm.userName" placeholder="帐号">
                  <template slot="prepend">
                    <img class="iconInput" src="@/assets/img/head.png" alt />
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input v-model="dataForm.password" type="password" placeholder="密码">
                  <template slot="prepend">
                    <img class="iconInput" src="@/assets/img/lock.png" alt />
                  </template>
                </el-input>
              </el-form-item>
              <!-- <el-form-item prop="captcha">
              <el-row :gutter="20">
                <el-col :span="14">
                  <el-input v-model="dataForm.captcha" placeholder="验证码">
                  </el-input>
                </el-col>
                <el-col :span="10" class="login-captcha">
                  <img :src="captchaPath" @click="getCaptcha()" alt="">
                </el-col>
              </el-row>
              </el-form-item>-->
              <el-form-item>
                <el-checkbox v-model="checked" style="margin-left:2px">记住用户名和密码</el-checkbox>
              </el-form-item>
              <el-form-item>
                <el-button class="login-btn-submit" type="primary" @click="dataFormSubmit()">登录</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getUUID } from "@/utils";
export default {
  data() {
    return {
      checked: false,
      dataForm: {
        userName: "",
        password: "",
        uuid: "",
        captcha: ""
      },
      dataRule: {
        userName: [
          { required: true, message: "帐号不能为空", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      },
      captchaPath: ""
    };
  },
  created() {
    this.getCaptcha();
  },
  methods: {
    // 提交表单

    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          localStorage.ischecked = this.checked ? "1" : "2";
          if (this.checked) {
            localStorage.userName = this.dataForm.userName;
            localStorage.password = this.dataForm.password;
          }
          this.$http({
            url: this.$http.adornUrl("/login"),
            method: "post",
            data: this.$http.adornData({
              username: this.dataForm.userName,
              password: this.dataForm.password,
              uuid: this.dataForm.uuid,
              captcha: this.dataForm.captcha
            })
          })
            .then(({ data }) => {
              if (data && data.result == 1) {
                this.$cookie.set("token", data.token);

                this.$router.replace({ name: "home" });
              } else {
                this.getCaptcha();
                this.$message.error(data.message);
              }
            })
            .catch(() => {
              this.$message.error("服务器错误，请联系管理员");
            });
        }
      });
    },
    // 获取验证码
    getCaptcha() {
      this.dataForm.uuid = getUUID();
      this.checked = localStorage.ischecked == "1" ? true : false;
      if (this.checked) {
        this.dataForm.userName = localStorage.userName;
        this.dataForm.password = localStorage.password;
      }
    }
  }
};
</script>

<style lang="scss">
.site-wrapper.site-page--login {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  // background-color: rgba(38, 50, 56, 0.6);
  overflow: hidden;
  &:before {
    position: fixed;
    top: 0;
    left: 0;
    z-index: -1;
    width: 100%;
    height: 100%;
    content: "";
    background-image: url(~@/assets/img/banner.png);
    background-size: cover;
  }
  .site-content__wrapper {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 0;
    margin: 0;
    overflow-x: hidden;
    overflow-y: auto;
    background-color: transparent;
  }
  .site-content {
    min-height: 100%;
    padding: 30px 30px 30px 30px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .brand-info {
    margin: 220px 100px 0 90px;
    color: #fff;
  }
  .brand-info__text {
    margin: 0 0 22px 0;
    font-size: 48px;
    font-weight: 400;
    text-transform: uppercase;
  }
  .brand-info__intro {
    margin: 10px 0;
    font-size: 16px;
    line-height: 1.58;
    opacity: 0.6;
  }
  .login-main {
    background-color: #fff;
    width: 800px;
    border-radius: 10px;
    overflow: hidden;
    display: flex;
  }
  .leftBg {
    width: 400px;
    display: flex;
    flex-direction: column;
    background: rgba(28, 132, 255, 1);
    align-items: center;
    padding-top: 98px;

    img {
      width: 28px;
      height: 28px;
      margin-left: 20px;
    }
  }
  .leftTit {
    width: 304px;
    text-align: left;
    font-size: 28px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: rgba(255, 255, 255, 1);
    line-height: 46px;
  }
  .linse {
    width: 304px;
    height: 1px;
    background: rgba(255, 255, 255, 1);
    opacity: 0.6;
    border-radius: 1px;
    margin-top: 23px;
    margin-bottom: 23px;
  }
  .mainTit {
    width: 310px;
    height: 34px;
    font-size: 36px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: rgba(255, 255, 255, 1);
    line-height: 46px;
  }
  .el-form-item__content {
    height: 50px;
    line-height: 50px;
    .el-input-group__prepend {
      box-sizing: border-box;
      width: 60px;
      height: 50px;
      line-height: 50px;
      padding: 0;
      text-align: center;
      border: none;
      background: rgba(221, 221, 221, 1);
    }
    .el-input-group {
      height: 50px;
      line-height: 50px;
      box-sizing: border-box;
      input {
        height: 50px;
        line-height: 50px;
      }
    }
  }
  .login-title {
    font-size: 36px;
    text-align: center;
    font-weight: 400;
    color: #1d9bf0;
    background-color: #f1f6f9;
    margin: 0;
    padding: 30px;
  }
  .logoImg {
    display: block;
    width: 93px;
    // height: 46px;
    margin: 15px auto 5px;
  }
  .logFrom {
    padding: 20px 40px 20px;
  }
  .login-captcha {
    overflow: hidden;
    > img {
      width: 100%;
      cursor: pointer;
    }
  }
  .login-btn-submit {
    width: 100%;
    margin-top: 10px;
    height: 45px;
    font-size: 18px;
    letter-spacing: 5px;
  }
}
.iconInput {
  width: 22px;
  height: 22px;
}
</style>
